home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 July / Macworld (1999-07).dmg / Shareware World / Info / For Developers / FrontierMenu / frontierMenu.tcl < prev    next >
Text File  |  1999-04-30  |  26KB  |  919 lines

  1. ## -*-Tcl-*-
  2.  # ###################################################################
  3.  #  Frontier menu - tools for using Alpha as Frontier's external editor
  4.  # 
  5.  #  FILE: "frontierMenu.tcl"
  6.  #                                    created: 97-04-03 22.01.22 
  7.  #                                last update: 99-04-30 20.23.44 
  8.  #  Author: Johan Linde
  9.  #  E-mail: <jlinde@telia.com>
  10.  #     www: <http://www.theophys.kth.se/~jl/Alpha.html>
  11.  #  
  12.  # Version: 2.1.5
  13.  # 
  14.  # Copyright 1997-1999 by Johan Linde
  15.  #  
  16.  # Much of the tcl code and the Frontier scripts have been written by 
  17.  # Danis Georgiadis <dmg@hyper.gr>
  18.  # 
  19.  # This software may be used freely, and distributed freely, as long as the 
  20.  # receiver is not obligated in any way by receiving it.
  21.  #  
  22.  # If you make improvements to this file, please share them!
  23.  # 
  24.  # ###################################################################
  25.  ##
  26.  
  27. if {[alpha::package vsatisfies ${alpha::version} 7.1b8]} {
  28. alpha::menu frontierMenu 2.1.5 global "•142" {} {frontierMenu} {
  29.     catch {removeMenu $frontierScriptMenu}
  30. } uninstall {
  31.     removeFile $pkg_file
  32.     removeFile "$HOME:Help:Frontier Help"
  33. } maintainer {
  34.     "Johan Linde" jlinde@telia.com <http://www.theophys.kth.se/~jl/Alpha.html>
  35. } help {file "Frontier Help"}
  36. } else {
  37. ;alpha::menu frontierMenu 2.1.5 "•142" in_menu {
  38. } uninstall {
  39.     removeFile $pkg_file
  40.     removeFile "$HOME:Help:Frontier Help"
  41. } maintainer {
  42.     "Johan Linde" jlinde@telia.com <http://www.theophys.kth.se/~jl/Alpha.html>
  43. } help {file "Frontier Help"}
  44. }
  45.  
  46. proc frontierMenu {} {}
  47. set frontierScriptMenu •144
  48.  
  49.  
  50. # Preferences
  51. newPref f autoLaunch 0 Fron
  52. newPref v BrowsePoints {{root root} {Websites websites}} Fron
  53. newPref v OpenPoints {{Websites websites} {Glossary user.html.glossary} {Templates user.html.templates}} Fron
  54.  
  55. # Register hooks
  56. hook::register closeHook frontierCloseHook
  57. hook::register savePostHook frontierSavePostHook
  58. hook::register saveasHook frontierSaveasHook
  59.  
  60. if {[alpha::package vsatisfies ${alpha::version} 7.1b1]} {
  61. proc frontierBrowseMenu {} {
  62.     global FronmodeVars
  63.     set bl {}
  64.     foreach b $FronmodeVars(BrowsePoints) {
  65.         lappend bl [lindex $b 0]
  66.     }
  67.     return [list Menu -n Browse -p frontierMenuProc -m [concat $bl [list "(-" "Browse at…" Add… Remove…]]]
  68. }
  69.  
  70. proc frontierOpenMenu {} {
  71.     global FronmodeVars
  72.     set bl {}
  73.     foreach b $FronmodeVars(OpenPoints) {
  74.         lappend bl [lindex $b 0]
  75.     }
  76.     return [list Menu -n Open -p frontierMenuProc -m [concat $bl [list "(-" "Open…" Add… Remove…]]]
  77. }
  78. } else {
  79. proc frontierBrowseMenu {} {
  80.     global FronmodeVars
  81.     set bl {}
  82.     foreach b $FronmodeVars(BrowsePoints) {
  83.         lappend bl [lindex $b 0]
  84.     }
  85.     return [list menu -n Browse -p frontierMenuProc -m [concat $bl [list "(-" "Browse at…" Add… Remove…]]]
  86. }
  87.  
  88. proc frontierOpenMenu {} {
  89.     global FronmodeVars
  90.     set bl {}
  91.     foreach b $FronmodeVars(OpenPoints) {
  92.         lappend bl [lindex $b 0]
  93.     }
  94.     return [list menu -n Open -p frontierMenuProc -m [concat $bl [list "(-" "Open…" Add… Remove…]]]
  95. }
  96. }
  97.  
  98. # Menu definition
  99. proc menu::buildFrontierMenu {} {
  100.     global frontierMenu
  101.     return [list build [list  \
  102.     "<U<O/Fswitch toFrontier" \
  103.     "<U<O/'previewPage" \
  104.     "<B<O/'publishPage" \
  105.     "<I<O/YfrontierShell" \
  106.     [frontierBrowseMenu] \
  107.     [frontierOpenMenu] \
  108.     "rebuildScriptsMenu" \
  109.     preferences…] \
  110.     frontierMenuProc "" $frontierMenu]
  111. }
  112.  
  113. menu::buildProc frontierMenu menu::buildFrontierMenu
  114. menu::buildSome frontierMenu
  115.  
  116. proc frontierMenuProc {menu item} {
  117.     global frontierMenu FronmodeVars
  118.     switch -glob $menu {
  119.         •* {
  120.             switch -glob $item {
  121.                 "switch*toFrontier" {frontierLaunch Fore}
  122.                 preferences {FronmodifyFlags}
  123.                 default {eval frontier$item}
  124.             }
  125.         }
  126.         Browse {
  127.             switch $item {
  128.                 "Browse at" {frontierBrowseAt}
  129.                 Add {frontierAddPoint Browse}
  130.                 Remove {frontierRemovePoint Browse}
  131.                 default {
  132.                     foreach b $FronmodeVars(BrowsePoints) {
  133.                         if {[lindex $b 0] == $item} {
  134.                             frontierCheckExist [lindex $b 1] $item Browse
  135.                             odbBrowse [lindex $b 1]
  136.                             break
  137.                         }
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.         Open {
  143.             switch $item {
  144.                 Open {frontierOpen}
  145.                 Add {frontierAddPoint Open}
  146.                 Remove {frontierRemovePoint Open}
  147.                 default {
  148.                     foreach b $FronmodeVars(OpenPoints) {
  149.                         if {[lindex $b 0] == $item} {
  150.                             frontierCheckExist [lindex $b 1] $item Open
  151.                             frontierDoScript "edit (@[lindex $b 1])" front
  152.                             break
  153.                         }
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159. }
  160.  
  161. proc frontierCheckExist {item mitem type} {
  162.     if {[frontierDoScript defined($item)] == "false"} {
  163.         alertnote "$mitem no longer exists in the database. It is removed from the menu."
  164.         frontierDoTheRemove $type $mitem
  165.         error ""
  166.     }
  167.     
  168. }
  169. # Called by Frontier when opening a Frontier text document in Alpha.
  170. proc openFromFrontier {} {
  171.     global frontierWinList
  172.     set name [lindex [winNames -f] 0]
  173.     set name0 [stripNameCount $name]
  174.     regsub -all {\[|\]} $name0 {\\&} name0
  175.     if {[lsearch -exact $frontierWinList $name0] < 0} {lappend frontierWinList $name0}
  176. }
  177.  
  178. # If the current document is a Frontier document, it is updated in Frontier.
  179. if {[alpha::package vsatisfies ${alpha::version} 7.2]} {
  180. proc frontierSavePostHook {name} {
  181.     global frontierWinList
  182.     regsub -all {\[|\]} $name {\\&} name
  183.     if {[lsearch -exact $frontierWinList $name] >= 0} {
  184.         frontierLaunch
  185.         regsub -all {\\([][])} $name {\1} name
  186.         AEBuild 'LAND' ALFA FMod "----" "“${name}”"
  187.     }
  188. }
  189. } else {
  190. ;proc frontierSavePostHook {name} {
  191.     global frontierWinList
  192.     if {[lsearch -exact $frontierWinList $name] >= 0} {
  193.         frontierLaunch
  194.         regsub -all {\\([][])} $name {\1} name
  195.         AEBuild 'LAND' ALFA FMod "----" "“${name}”"
  196.     }
  197. }
  198. }
  199.  
  200. proc frontierLaunch {{b Back}} {
  201.     if {![app::isRunning LAND]} {
  202.         if {[catch {eval app::launch$b LAND}]} {
  203.             alertnote "Could not launch Frontier."
  204.             error "Launch error"
  205.         }
  206.     } elseif {$b == "Fore"} {
  207.         switchTo 'LAND'
  208.     }
  209. }
  210.  
  211. # A list of windows opened from Frontier.
  212. if {![info exists frontierWinList]} {set frontierWinList {}}
  213.  
  214. # Executes a script in Frontier.
  215. proc frontierDoScript {script {front 0} {alert 1} {queue 0}} {
  216.     if {[catch frontierLaunch]} {error "Could not launch Frontier."}
  217.     if {$queue} {
  218.         # Never switch to Frontier when queing.
  219.         dosc -c 'LAND' -q -t 30000 -s $script
  220.         return
  221.     } elseif {[catch {dosc -c 'LAND' -s $script} returnvalue]} {
  222.         if {$alert} {
  223.             alertnote "Frontier $returnvalue"
  224.             error "Frontier $returnvalue"
  225.         }
  226.         error $returnvalue
  227.     } elseif {$front == "front"} {
  228.         switchTo 'LAND'
  229.     }
  230.     return $returnvalue
  231. }
  232.  
  233. # Executes one of the scripts in Frontier, which are required to use Alpha with Frontier.
  234. proc frontierDoAlphaScript {script {queue 0}} {
  235.     global HOME frontierHasWarned
  236.     if {[catch {frontierDoScript $script 0 0 $queue} res]} {
  237.         frontierError
  238.         error $res
  239.     }
  240.     return $res
  241. }
  242.  
  243. proc frontierError {} {
  244.     global frontierHasWarned
  245.     if {![info exists frontierHasWarned]} {
  246.         alertnote "The Frontier verbs required to integrate Alpha and Frontier have not been\
  247.           properly installed. See the file 'Frontier Help.'"
  248.         edit -r -c "$HOME:Help:Frontier Help"
  249.     }
  250. }
  251.     
  252. # closeHook
  253. # If the window to be closed is a Frontier document, it is removed
  254. # from Frontier's list of open external documents.
  255. proc frontierCloseHook {name} {
  256.     global frontierWinList frontierQSWin frontierCommandHistory frontierCommandNum
  257.     if {[set where [lsearch -exact $frontierWinList $name]] >= 0} {
  258.         regsub -all {\\([][])} $name {\1} name
  259.         set frontierWinList [lreplace $frontierWinList $where $where]
  260.         catch {AEBuild 'LAND' ALFA FCls "----" "“${name}”"}
  261.     }
  262.     if {$name == $frontierQSWin} {set frontierCommandHistory ""; set frontierCommandNum 0}
  263. }
  264.  
  265. # saveasHook
  266. proc frontierSaveasHook {oldname newname} {
  267.     frontierCloseHook $oldname
  268. }
  269.  
  270.  
  271. # Does the same as 'Preview Page' in Frontier's web menu.
  272. proc frontierpreviewPage {} {
  273.     frontierPrePub viewInBrowser
  274. }
  275.  
  276. # Does the same as 'Publish Page' in Frontier's web menu.
  277. proc frontierpublishPage {} {
  278.     frontierPrePub publishPage
  279. }
  280.  
  281. proc frontierPrePub {script} {
  282.     global frontierWinList
  283.     if {![llength [winNames]]} {
  284.         alertnote "No window!"
  285.         return
  286.     }
  287.     set name [lindex [winNames -f] 0]
  288.     set name0 [stripNameCount $name]
  289.     regsub -all {\[|\]} $name0 {\\&} name0
  290.     if {[lsearch -exact $frontierWinList $name0] >= 0} {
  291.         if {[winDirty]} {
  292.             if {[set ask [askyesno -c "Save '[file tail $name]'?"]] == "yes"} {
  293.                 save
  294.             } elseif {$ask == "cancel"} {
  295.                 return
  296.             }
  297.         }
  298.         regsub -all "\"" $name0 "\\\"" name0
  299.         frontierDoScript "Alpha.${script}(\"[string tolower $name0]\")"
  300.     } else {
  301.         alertnote "Not a Frontier window."
  302.     }
  303. }
  304.  
  305. # Open a window in Frontier
  306. proc frontierOpen {} {
  307.     if {![catch {frontierGetAddress} addr]} {
  308.         frontierDoScript "edit (@$addr)" front
  309.     }
  310. }
  311.  
  312. # Browse a table in Frontier
  313. proc frontierBrowseAt {} {
  314.     if {![catch {frontierGetAddress} addr]} {
  315.         odbBrowse $addr
  316.     }
  317. }
  318.  
  319. # Add to Browse and Open submenus
  320. proc frontierAddPoint {type} {
  321.     global FronmodeVars modifiedModeVars
  322.     set values ""
  323.     while {1} {
  324.         set values [dialog -w 450 -h 130 -t "Add $type menu item" 30 10 290 30 \
  325.             -t "Location in database:" 10 40 160 60 -e [lindex $values 0] 165 40 440 55 \
  326.             -t "Menu text:" 78 70 160 90 -e [lindex $values 1] 165 70 440 85 \
  327.             -b OK 20 100 85 120 -b Cancel 105 100 170 120]
  328.         if {[lindex $values 3]} {return}
  329.         set addr [string trim [lindex $values 0]]
  330.         if {$addr == ""} {alertnote "Location is database must be specified."; continue}
  331.         set text [string trim [lindex $values 1]]
  332.         if {$text == ""} {alertnote "The menu item must be specified."; continue}
  333.         if {[frontierDoScript "defined($addr)"] == "true"} {
  334.             set ex 0
  335.             foreach b $FronmodeVars(${type}Points) {
  336.                 if {[lindex $b 0] == $text} {alertnote "A menu item '$text' already exists."; set ex 1}
  337.             }
  338.             if {!$ex} {break}
  339.         } else {
  340.             alertnote "“$addr” is not a valid database address."
  341.         }
  342.     }
  343.     lappend FronmodeVars(${type}Points) [list $text $addr]
  344.     lappend modifiedModeVars [list ${type}Points FronmodeVars]
  345.     eval [eval frontier${type}Menu]
  346. }
  347.  
  348. # Remove from Browse and Open submenus.
  349. proc frontierRemovePoint {type} {
  350.     global FronmodeVars
  351.     set points {}
  352.     foreach b $FronmodeVars(${type}Points) {
  353.         lappend points [lindex $b 0]
  354.         set pointat([lindex $b 0]) [lindex $b 1]
  355.     }
  356.     if {![llength $points] || [catch {listpick -p "Select [string tolower $type] point to remove:" -l $points} points] ||
  357.         ![llength $points]} {return}
  358.     set points [lindex $points 0]
  359.     if {[askyesno "'$points' points to '$pointat($points)'. Remove?"] != "yes"} {return}
  360.     frontierDoTheRemove $type $points
  361. }
  362.  
  363. proc frontierDoTheRemove {type points} {
  364.     global FronmodeVars modifiedModeVars
  365.     set n {}
  366.     foreach b $FronmodeVars(${type}Points) {
  367.         if {[lindex $b 0] != $points} {lappend n $b}
  368.     }
  369.     set FronmodeVars(${type}Points) $n
  370.     lappend modifiedModeVars [list ${type}Points FronmodeVars]
  371.     eval [eval frontier${type}Menu]
  372. }
  373.  
  374. proc frontierGetAddress {} {
  375.     while {1} {
  376.         if {[catch {set addr [prompt "Location in Frontier database:" ""]}]} {
  377.             error ""
  378.         } else {
  379.             set addr [string trimleft [string trim $addr] {@}]
  380.             switch [frontierDoScript "defined($addr)"] {
  381.                 "true"        {return $addr}
  382.                 "false"        {alertnote "“$addr” is not a valid database address"}
  383.                 ""            {error ""}
  384.             }
  385.         }
  386.     }
  387. }
  388.  
  389. proc FronmodifyFlags {} {
  390.     global FronmodeVars modifiedModeVars
  391.     set values [dialog -w 300 -h 110 -t "Frontier Preferences" 30 10 290 30 \
  392.         -c "Launch Frontier at startup" $FronmodeVars(autoLaunch) 10 40 290 60 \
  393.         -b OK 20 80 85 100 -b Cancel 105 80 170 100]
  394.     if {[lindex $values 2]} {return}
  395.     set i -1
  396.     foreach flag [list autoLaunch] {
  397.         global $flag
  398.         incr i
  399.         set val [lindex $values $i]
  400.         if {$FronmodeVars($flag) != $val} {
  401.             set $flag $val
  402.             set FronmodeVars($flag) $val
  403.             lappend modifiedModeVars [list $flag FronmodeVars]
  404.         }
  405.     }
  406. }
  407.  
  408. proc OdbmodifyFlags {} {
  409.     FronmodifyFlags
  410. }
  411.  
  412. proc frontierGetWin {} {
  413.     global frontierWinList frontierWinNum
  414.     if {![info exists frontierWinNum]} {set frontierWinNum 0}
  415.     if {[llength $frontierWinList] == $frontierWinNum} {
  416.         unset frontierWinNum
  417.         return ""
  418.     } else {
  419.         return [lindex $frontierWinList [expr [incr frontierWinNum] - 1]]
  420.     }
  421. }
  422.  
  423. proc frontierCloseAllWindows {} {
  424.     global frontierWinList
  425.     foreach win $frontierWinList {
  426.         regsub -all {\\([][])} $win {\1} win
  427.         bringToFront $win
  428.         setWinInfo dirty 0
  429.         killWindow
  430.     }
  431. }
  432.  
  433. #===============================================================================
  434. # Script menu
  435. # The code to extract a Frontier menu has been written by
  436. # Danis Georgiadis <dmg@hyper.gr>
  437. #===============================================================================
  438.  
  439. proc setFrontierMenuScript {menu item scpt} {
  440.     global frontierMenuScripts
  441.     if {[regexp {&$} $item]} {
  442.         set item [string trimright $item &]
  443.     } else {
  444.         regsub -all {<[BUISEO]} $item "" item
  445.         regsub {/[a-zA-Z]} $item "" item
  446.         regsub -all {[!\^].} $item "" item
  447.     }
  448.     set key [string trimright "$menu$item" …]
  449.     set frontierMenuScripts($key) $scpt
  450. }
  451.  
  452. proc frontierBuildScriptMenu {} {
  453.     global frontierScriptMenu FronmodeVars
  454.  
  455.     if {![app::isRunning LAND]} {
  456.         if {$FronmodeVars(autoLaunch)} {
  457.             app::launchBack LAND
  458.         } else {
  459.             return
  460.         }
  461.     }
  462.     currentReplyHandler frontierGetMenuReplyHandler
  463.     frontierDoAlphaScript "Alpha.getMenuSource()" 1
  464.  
  465. }
  466.  
  467. proc frontierScriptMenuProc {menu item} {
  468.     global frontierMenuScripts frontierScriptMenu
  469.     if {$menu == $frontierScriptMenu} {set menu ""}
  470.     set key "$menu$item"
  471.     frontierDoScript $frontierMenuScripts($key)
  472. }
  473.  
  474. proc frontierrebuildScriptsMenu {} {
  475.     global frontierMenuScripts
  476.     frontierLaunch
  477.     currentReplyHandler frontierInvalReplyHandler
  478.     frontierDoAlphaScript "Alpha.invalMenuSources()" 1
  479. }
  480.  
  481. proc frontierGetMenuReplyHandler {args} {
  482.     global frontierScriptMenu
  483.     if {[string range $args 12 16] == "errs:"} {
  484.         frontierError
  485.     } else {
  486.         regexp {“([^”]*)”} $args dum txt
  487.         regsub -all {\\\{} $txt "{" txt
  488.         regsub -all {\\\}} $txt "}" txt
  489.         menu -m -n $frontierScriptMenu -p frontierScriptMenuProc $txt
  490.         insertMenu $frontierScriptMenu
  491.          currentReplyHandler frontierGetDefsReplyHandler
  492.         catch {frontierDoAlphaScript "Alpha.getDefsSource()" 1}
  493.     }
  494.     return 1
  495. }
  496.  
  497. proc frontierGetDefsReplyHandler {args} {
  498.     if {[string range $args 12 16] == "errs:"} {
  499.         frontierError
  500.     } else {
  501.         regexp {“([^”]*)”} $args dum txt
  502.         regsub -all {\\\{} $txt "{" txt
  503.         regsub -all {\\\}} $txt "}" txt
  504.         catch {eval $txt}
  505.     }
  506.     message "Frontier script menu built."
  507.     return 1    
  508. }
  509.  
  510. proc frontierInvalReplyHandler {args} {
  511.     catch {unset frontierMenuScripts}
  512.     catch {frontierBuildScriptMenu}
  513.     return 1
  514. }
  515.  
  516.  
  517. #===============================================================================
  518. #
  519. # Frontier shell
  520. # Some ideas taken from Matlab mode by Stephen Merkowitz
  521. #===============================================================================
  522. set frontierQSWin "* Frontier shell *"
  523. set frontierCommandHistory ""
  524. set frontierCommandNum 0
  525.  
  526. proc frontierfrontierShell {} {
  527.     global frontierQSWin
  528.     
  529.     if {[lsearch [winNames] $frontierQSWin] >= 0} {
  530.         bringToFront $frontierQSWin
  531.     } else {
  532.         new -n $frontierQSWin -m Fron
  533.         setWinInfo -w $frontierQSWin shell 1
  534.         insertText "Welcome to Alpha's Frontier shell\r«» "
  535.     }
  536. }
  537.  
  538.  
  539. proc frontierRunQuickScript {} {
  540.     global frontierCommandHistory frontierCommandNum frontierQSWin
  541.     set pos [getPos]
  542.  
  543.     set ind [string first "«» " [getText [lineStart $pos] [nextLineStart [getPos]]]]
  544.     if {$ind >= 0} {
  545.         set lStart [expr [lineStart $pos]+$ind+2]
  546.         endOfLine
  547.         set scriptName [getText $lStart [getPos]]
  548.         if {[getPos] != [maxPos]} {
  549.             goto [maxPos]
  550.             insertText $scriptName
  551.         }
  552.         
  553.         if {[string trim $scriptName] != ""} {
  554.             catch {frontierDoScript $scriptName 0 0} result
  555.             if {[string compare [lindex $frontierCommandHistory [expr [llength $frontierCommandHistory]-1]] $scriptName] != 0} {
  556.                 lappend frontierCommandHistory $scriptName
  557.                 if {[llength $frontierCommandHistory] > 30} {
  558.                     set frontierCommandHistory [lrange $frontierCommandHistory 1 end]
  559.                 }
  560.             }
  561.             set frontierCommandNum [llength $frontierCommandHistory]
  562.         } else {
  563.             set result ""
  564.         }
  565.         if {[string length $result]} {
  566.             insertText -w $frontierQSWin "\r" $result \r "«» "
  567.         } else {
  568.             insertText -w $frontierQSWin \r "«» "
  569.         }
  570.     } else {
  571.            if {[getPos] == [maxPos]} {
  572.             insertText "«» "
  573.         } else {
  574.             bind::CarriageReturn
  575.         }
  576.     }
  577.     return
  578. }
  579.  
  580.  
  581. proc frontierPrevCommand {} {
  582.     global frontierCommandHistory frontierCommandNum
  583.     
  584.     set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
  585.     if {[set ind [string first "«» " $text]] == 0} {
  586.         goto [expr [lineStart [getPos]] + $ind + 2]
  587.     } else return
  588.  
  589.     incr frontierCommandNum -1
  590.     if {$frontierCommandNum < 0} {
  591.         incr frontierCommandNum
  592.         endOfLine
  593.         return
  594.     }
  595.     set text [lindex $frontierCommandHistory $frontierCommandNum]
  596.     set to [nextLineStart [getPos]]
  597.     if {[lookAt [expr $to-1]] == "\r"} {incr to -1}
  598.     replaceText [getPos] $to $text
  599. }
  600.  
  601.  
  602. proc frontierNextCommand {} {
  603.     global frontierCommandHistory frontierCommandNum
  604.     
  605.     set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
  606.     if {[set ind [string first "«» " $text]] == 0} {
  607.         goto [expr [lineStart [getPos]] + $ind + 2]
  608.     } else return
  609.  
  610.     incr frontierCommandNum
  611.     if {$frontierCommandNum >= [llength $frontierCommandHistory]} {
  612.         incr frontierCommandNum -1
  613.         frontierCancelLine
  614.         return
  615.     }
  616.     set text [lindex $frontierCommandHistory $frontierCommandNum]
  617.     set to [nextLineStart [getPos]]
  618.     if {[lookAt [expr $to-1]] == "\r"} {incr to -1}
  619.     replaceText [getPos] $to $text
  620. }
  621.  
  622. proc frontierCancelLine {} {
  623.     global frontierCommandHistory frontierCommandNum
  624.  
  625.     set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
  626.     if {[set ind [string first "«» " $text]] == 0} {
  627.         goto [expr [lineStart [getPos]] + $ind + 3]
  628.     } else return
  629.     
  630.     set to [nextLineStart [getPos]]
  631.     deleteText [getPos] $to
  632.     
  633.     set frontierCommandNum [llength $frontierCommandHistory]
  634. }
  635.  
  636. proc frontierBol {} {
  637.     set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
  638.     if {[set ind [string first "«» " $text]] == 0} {
  639.         goto [expr [lineStart [getPos]] + $ind + 3]
  640.     } else {
  641.         goto [lineStart [getPos]]
  642.     }
  643. }
  644.  
  645. proc Fron::OptionTitlebar {} {
  646.     global frontierCommandHistory
  647.     return $frontierCommandHistory
  648. }
  649.  
  650. proc Fron::OptionTitlebarSelect {item} {
  651.     insertText [string range $item [expr 1+[string first " " $item]] end]
  652.     if {[key::optionPressed]} {frontierRunQuickScript}
  653. }
  654.  
  655. regModeKeywords -m {«} Fron {}
  656. bind up <z> frontierPrevCommand Fron
  657. bind down <z> frontierNextCommand Fron
  658. bind '\r' frontierRunQuickScript Fron
  659. bind 'u'  <z>  frontierCancelLine  Fron
  660. bind left <c> frontierBol Fron
  661. bind 'a' <z> frontierBol Fron
  662.  
  663. #===============================================================================
  664. # Odb browser
  665. # Written by Danis Georgiadis <dmg@hyper.gr> and modified by me to be integrated 
  666. # with the rest.
  667. #===============================================================================
  668.  
  669. set odbBrowserTabLength 3
  670. set odbBrowserTypeOffset 60
  671.  
  672. proc odbget120Spaces {} {
  673.     set spaces40 "                                        "
  674.     return "$spaces40$spaces40$spaces40"
  675. }
  676.  
  677. proc odbGetIndLevel {indStr} {
  678.     global odbBrowserTabLength
  679.     return [expr [string length $indStr] / $odbBrowserTabLength]
  680. }
  681.  
  682. proc odbGetIndString {indLevel} {
  683.     global odbBrowserTabLength
  684.     return [string range [odbget120Spaces] 0 [expr [expr $indLevel * $odbBrowserTabLength] - 1]]
  685. }
  686.  
  687. proc odbGetNextIndString {thisIndStr} {
  688.     return [odbGetIndString [expr [odbGetIndLevel $thisIndStr] + 1]]
  689. }
  690.  
  691. proc odbBrowseGetLineParts {name type addr level} {
  692.     global odbBrowserTypeOffset
  693.     global odbBrowserTabLength
  694.     
  695.     set indPadPart [odbGetIndString $level]
  696.     set namePart [string trim $name "\t "]
  697.     set typePadSize [expr $odbBrowserTypeOffset - [expr [string length $indPadPart] + [string length $name]]]
  698.     set typePadPart [string range [odbget120Spaces] 0 [expr $typePadSize - 1]]
  699.     set typePart "◊$type◊"
  700.     set addrPart "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t∞$addr∞"
  701.     
  702.     set res ""
  703.     lappend res $indPadPart $namePart $typePadPart $typePart $addrPart
  704.     
  705.     return $res
  706. }
  707.  
  708. proc odbBrowseDown {} {
  709.     set curPos [getPos]
  710.     set curLineStart [lineStart $curPos]
  711.     set curLineEnd [nextLineStart $curPos]
  712.     select $curLineStart $curLineEnd
  713.     
  714.     set newLineStart [nextLineStart $curLineStart]
  715.     set newLineEnd [nextLineStart $newLineStart]
  716.     if {$newLineStart < [maxPos]} {
  717.         select $newLineStart $newLineEnd
  718.     }
  719. }
  720.  
  721. proc odbBrowseCmdDown {{option 0}} {
  722.     set curPos [getPos]
  723.     set curLineStart [lineStart $curPos]
  724.     set curLineEnd [nextLineStart $curPos]
  725.     
  726.     if {[regexp {^( *).+◊tabl◊\t+∞(.+)∞} [getText $curLineStart $curLineEnd] junk ind addr]} {
  727.         if {[frontierDoScript "defined($addr)"] == "false"} {return}
  728.         if {$option} {killWindow}
  729.         odbBrowse $addr
  730.     }
  731. }
  732.  
  733. proc odbBrowseUp {} {
  734.     set curPos [getPos]
  735.     set curLineStart [lineStart $curPos]
  736.     set curLineEnd [nextLineStart $curPos]
  737.     select $curLineStart $curLineEnd
  738.     
  739.     set newLineStart [prevLineStart $curLineStart]
  740.     set newLineEnd [nextLineStart $newLineStart]
  741.     if {$newLineEnd > 0} {
  742.         select $newLineStart $newLineEnd
  743.     }
  744. }
  745.  
  746. proc odbBrowseCmdUp {{option 0}} {
  747.     regexp {∞(.+)∞} [getText 0 [nextLineStart 0]] junk addr
  748.     if {[set point [string last "." $addr]] >= 0} {
  749.         if {[frontierDoScript "defined($addr)"] == "false"} {return}
  750.         if {$option} {killWindow}
  751.         odbBrowse [string range $addr 0 [expr $point - 1]]
  752.     }
  753. }
  754.  
  755. proc odbBrowserAddCells {pos cells indLevel} {
  756.     
  757.     set tmp ""
  758.     set colorCodes ""
  759.     set lastPos $pos
  760.     
  761.     foreach cell $cells {
  762.         set cellName [lindex $cell 0]
  763.         set cellType [lindex $cell 1]
  764.         set cellAddr [lindex $cell 2]
  765.         
  766.         set parts [odbBrowseGetLineParts $cellName $cellType $cellAddr $indLevel]
  767.         
  768.         set indPart [lindex $parts 0]
  769.         set namePart [lindex $parts 1]
  770.         set typePartPad [lindex $parts 2]
  771.         set typePart [lindex $parts 3]
  772.         set addrPart [lindex $parts 4]
  773.         
  774.         set nameStart [expr $lastPos + [string length $indPart]]
  775.         set nameEnd [expr $nameStart + [string length $namePart]]
  776.         
  777.         if {$cellType == "TEXT" || $cellType == "wptx"} {
  778.             lappend colorCodes [concat $nameStart 3]
  779.             lappend colorCodes [concat $nameEnd 0]
  780.         } elseif {$cellType == "tabl"} {
  781.             lappend colorCodes [concat $nameStart 5]
  782.             lappend colorCodes [concat $nameEnd 0]
  783.         } else {
  784.             lappend colorCodes [concat $nameStart 1]
  785.             lappend colorCodes [concat $nameEnd 0]
  786.         }
  787.         
  788.         set typeStart [expr $lastPos + [string length $indPart] + [string length $namePart] + [string length $typePartPad]]
  789.         set typeEnd [expr $typeStart + [string length $typePart]]
  790.         lappend colorCodes [concat $typeStart 4]
  791.         lappend colorCodes [concat $typeEnd 0]
  792.         
  793.         set line ""
  794.         append line $indPart $namePart $typePartPad $typePart $addrPart "\n"
  795.         append tmp $line
  796.         
  797.         set lastPos [expr $lastPos + [string length $line]]
  798.     }
  799.     
  800.     select $pos $pos
  801.     setWinInfo read-only 0
  802.     
  803.     insertText $tmp
  804.     
  805.     foreach colorCode $colorCodes {
  806.         insertColorEscape [lindex $colorCode 0] [lindex $colorCode 1]
  807.     }
  808.     
  809.     setWinInfo dirty 0
  810.     setWinInfo read-only 1
  811.     eval sizeWin [lrange [getGeometry] 2 end]
  812. }
  813.  
  814. proc odbBrowseRight {} {
  815.     set curPos [getPos]
  816.     set curLineStart [lineStart $curPos]
  817.     set curLineEnd [nextLineStart $curPos]
  818.     
  819.     if {[regexp {^( *).+◊tabl◊\t+∞(.+)∞} [getText $curLineStart $curLineEnd] junk ind addr]} {
  820.         set nextIndString [odbGetNextIndString $ind]
  821.         set nextLineText [getText [nextLineStart $curLineStart] [nextLineStart [nextLineStart $curLineStart]]]
  822.         if {![regexp "^$nextIndString" $nextLineText junk]} {
  823.             if {[frontierDoScript "defined($addr)"] == "false"} {return}            
  824.             set cells [frontierDoAlphaScript "Alpha.getCellData(@$addr)"]
  825.             odbBrowserAddCells $curLineEnd $cells [odbGetIndLevel $nextIndString]
  826.             
  827.         }
  828.     }
  829.     
  830.     select $curLineStart $curLineEnd
  831. }
  832.  
  833. proc odbBrowseLeft {} {
  834.     set curPos [getPos]
  835.     set curLineStart [lineStart $curPos]
  836.     set curLineEnd [nextLineStart $curPos]
  837.     
  838.     if {[regexp {^( *).+∞(.+)∞} [getText $curLineStart $curLineEnd] junk ind elems]} {
  839.         set pos [nextLineStart $curLineStart]
  840.         set start $pos
  841.         set nextIndString [odbGetNextIndString $ind]
  842.         while {[regexp "^$nextIndString" [getText $pos [nextLineStart $pos]] junk]} {
  843.             set pos [nextLineStart $pos]
  844.         }
  845.         setWinInfo read-only 0
  846.         deleteText $start $pos
  847.         setWinInfo dirty 0
  848.         setWinInfo read-only 1
  849.     }
  850.     select $curLineStart $curLineEnd
  851. }
  852.  
  853. proc odbBrowseEditObj {} {
  854.     set curPos [getPos]
  855.     set curLineStart [lineStart $curPos]
  856.     set curLineEnd [nextLineStart $curPos]
  857.     
  858.     if {[regexp {^.+∞(.+)∞} [getText $curLineStart $curLineEnd] junk addr]} {
  859.         frontierDoAlphaScript "Alpha.editCell(@$addr)"
  860.     }
  861. }
  862.  
  863. proc odbBrowse {{addr root}} {
  864.     if {$addr == ""} {
  865.         return
  866.     }
  867.     
  868.     global odbBrowserTypeOffset
  869.     global odbBrowserTabLength
  870.     
  871.     set cell [frontierDoAlphaScript "Alpha.getCellData(@$addr, false)"]
  872.     set wtitle [lindex [lindex $cell 0] 2]
  873.     regsub -all {[][]} $wtitle "" wtitle
  874.     set wtitle "* Frontier “$wtitle” *"
  875.     
  876.     if {[lsearch [winNames] $wtitle] >= 0} {
  877.         bringToFront $wtitle
  878.     } else {
  879.         new -n $wtitle -g 4 42 449 300 -m Odb
  880.         setWinInfo dirty 0
  881.         odbBrowserAddCells 0 $cell 0
  882.         select 0 [nextLineStart 0]
  883.         odbBrowseRight
  884.     }
  885. }
  886.  
  887. bind '\r'        odbBrowseEditObj    Odb
  888. bind enter        odbBrowseEditObj    Odb
  889.  
  890. bind down         odbBrowseDown        Odb
  891. bind down <c>    odbBrowseCmdDown    Odb
  892. bind down <co>    {odbBrowseCmdDown 1}    Odb
  893. bind up            odbBrowseUp            Odb
  894. bind up <c>        odbBrowseCmdUp        Odb
  895. bind up <co>    {odbBrowseCmdUp 1}        Odb
  896. bind right        odbBrowseRight        Odb
  897. bind left        odbBrowseLeft        Odb
  898.  
  899. if {![info exists frontierVersion] || $frontierVersion != 2.12} {
  900.     dialog -w 400 -h 180 -t "Welcome to Frontier menu 2.1.5" 70 10 390 30 \
  901.       -t "Make sure you install all the scripts in the folder 'Frontier verbs' into Frontier.\r\
  902.       If you upgrade from a previous version make sure you install the Frontier verbs which have been updated.\r\
  903.       You find information in the file 'Frontier Help'." 10 50 390 135 \
  904.       -b OK 20 150 85 170
  905.     catch {edit -r -c "$HOME:Help:Frontier Help"}
  906.     addDef frontierVersion 2.12
  907.     set frontierHasWarned 1
  908. }
  909.  
  910.  
  911. catch {frontierBuildScriptMenu}
  912. catch {unset frontierHasWarned}
  913.